﻿using FieldTools.Client.ViewModel;
using System;
using System.Collections;
using System.Windows;
using System.Windows.Controls;

namespace FieldTools.Client.Command
{
    /// <summary>
    /// 用户控件独立窗口管理器，写法不规划，后续有机会再优化
    /// </summary>
    public static class WindowControlManager
    {
        private static Hashtable _RegisterWindowControl = new Hashtable();
        private static Hashtable _RegisterVMControl = new Hashtable();
        /// <summary>
        /// 窗口注册器
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        public static void Register<T>(string key)
        {
            if (!_RegisterWindowControl.Contains(key))
            {
                _RegisterWindowControl.Add(key, typeof(T));
            }
        }
        /// <summary>
        /// 窗口注册器
        /// </summary>
        /// <typeparam name="T1"></typeparam>
        /// <typeparam name="T2"></typeparam>
        /// <param name="key"></param>
        /// <param name="vmkey"></param>
        public static void Register<T1, T2>(string key, string vmkey)
        {
            if (!_RegisterWindowControl.Contains(key))
            {
                _RegisterWindowControl.Add(key, typeof(T1));
            }
            if (!_RegisterVMControl.Contains(vmkey))
            {
                _RegisterVMControl.Add(vmkey, typeof(T2));
            }
        }
        /// <summary>
        /// 窗口注册器
        /// </summary>
        /// <param name="key"></param>
        /// <param name="t"></param>
        public static void Register(string key, Type t)
        {
            if (!_RegisterWindowControl.Contains(key))
            {
                _RegisterWindowControl.Add(key, t);
            }
        }
        /// <summary>
        /// 窗口注册器
        /// </summary>
        /// <param name="key"></param>
        /// <param name="vmkey"></param>
        /// <param name="t1"></param>
        /// <param name="t2"></param>
        public static void Register(string key, Type t1, string vmkey, Type t2)
        {
            if (!_RegisterWindowControl.Contains(key))
            {
                _RegisterWindowControl.Add(key, t1);
            }
            if (!_RegisterVMControl.Contains(vmkey))
            {
                _RegisterVMControl.Add(vmkey, t2);
            }
        }
        /// <summary>
        /// 移除注册的窗口
        /// </summary>
        /// <param name="key"></param>
        public static void Remove(string key)
        {
            if (_RegisterWindowControl.ContainsKey(key))
            {
                _RegisterWindowControl.Remove(key);
            }
        }

        /// <summary>
        /// 移除注册的窗口
        /// </summary>
        /// <param name="key"></param>
        /// <param name="vmkey"></param>
        public static void Remove(string key, string vmkey)
        {
            if (_RegisterWindowControl.ContainsKey(key))
            {
                _RegisterWindowControl.Remove(key);
            }
            if (_RegisterVMControl.ContainsKey(vmkey))
            {
                _RegisterVMControl.Remove(vmkey);
            }
        }

        public static UserControl GetShow(string key, string vmkey=null)
        {
            if (_RegisterWindowControl.ContainsKey(key))
            {
                var control = (UserControl)Activator.CreateInstance((Type)_RegisterWindowControl[key]);
                //control.DataContext = VM;
                if (vmkey != null)
                {
                    if (_RegisterVMControl.ContainsKey(vmkey))
                    {
                        var VM = (NotificationObject)Activator.CreateInstance((Type)_RegisterVMControl[vmkey]);
                        control.DataContext = VM;
                        return control;
                    }
                }
                return control;
            }
            return null;
        }
    }
}
